import {useState} from "react";
import {Input, View, Button, Picker} from "@tarojs/components";
import Taro, {useLoad} from '@tarojs/taro'
import './index.scss'
import {putRequest} from "../../util/request";


export default function Index() {

  const [phone, setPhone] = useState<string>('');
  const [signature, setSignature] = useState<string>('');
  const [birthday, setBirthday] = useState<string>('');

  useLoad((param) => {
    console.log('修改个人信息页面加载',param)
    setPhone(param.phone)
    setSignature(param.signature=='null'?'':param.signature)
    setBirthday(param.birthday)
  })

  const save = () => {
    if (!phone) {
      Taro.showToast({title: '手机号不能为空', icon: 'none'})
      return
    }
    putRequest('/sysUser/info', { signature,phone,birth:birthday }, (res) => {
      if (res.success) {
        Taro.showToast({title: '保存成功', icon: 'none'})
        setTimeout(()=>{
          Taro.navigateBack()
        },1500)
      } else {
        Taro.showToast({title: res.message, icon: 'none'})
      }
    })
  };

  return (
    <View className='login-page'>
      <View className='input-container'>
        <Input
          type='text'
          placeholder='个性签名'
          value={signature}
          onInput={e => setSignature(e.detail.value)}
          className='login-input'
        />
        <Input
          type='text'
          placeholder='手机号码'
          value={phone}
          onInput={e => setPhone(e.detail.value)}
          className='login-input'
        />
        <Picker mode='date' className='login-input' onChange={e => setBirthday(e.detail.value)}>
          <View className='picker'>
            <Input
              type='text'
              placeholder='选择出生日期'
              value={birthday}
              style={{marginTop: '10rpx'}}
              disabled
            />
          </View>
        </Picker>
      </View>
      <Button className='login-button' onClick={save}>保 存</Button>
    </View>
  )
}
